package com.drama.api.company.withdraw.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.drama.api.base.Constants;
import com.drama.api.base.Type;
import com.drama.api.company.bank.dao.CompanyBankDao;
import com.drama.api.company.bank.domain.po.CompanyBankPO;
import com.drama.api.company.company.dao.CompanyDao;
import com.drama.api.company.company.domain.po.CompanyPO;
import com.drama.api.company.withdraw.dao.CompanyWithdrawDao;
import com.drama.api.company.withdraw.domain.po.CompanyWithdrawPO;
import com.drama.api.company.withdraw.domain.vo.CompanyWithdrawAddVO;
import com.drama.api.company.withdraw.domain.vo.CompanyWithdrawCountVO;
import com.drama.api.company.withdraw.domain.vo.CompanyWithdrawSearchVO;
import com.drama.api.company.withdraw.domain.vo.CompanyWithdrawVO;
import com.drama.api.company.withdraw.service.ICompanyWithdrawService;
import com.drama.api.sys.message.service.ISysMessageService;
import com.drama.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;


@Service
@Transactional
@Slf4j
public class CompanyWithdrawServiceImpl implements ICompanyWithdrawService {

    @Autowired
    private CompanyWithdrawDao withdrawDao;
    @Autowired
    private CompanyDao companyDao;
    @Autowired
    private CompanyBankDao companyBankDao;
    @Autowired
    private ISysMessageService messageService;

    @Override
    public List<CompanyWithdrawVO> getList(CompanyWithdrawSearchVO searchVO) {
        if(StringUtils.isBlank(searchVO.getCompanyId())){
            searchVO.setCompanyId((String) StpUtil.getLoginId());
        }
        return withdrawDao.getList(searchVO);
    }

    public static void main(String[] args) {
        System.out.println(DateUtil.date());
        System.out.println(DateUtil.beginOfDay(DateUtil.parseDate(DateUtil.thisYear()+"-"+(DateUtil.thisMonth()+1)+"-25")));
        System.out.println(DateUtil.endOfDay(DateUtil.parseDate(DateUtil.thisYear()+"-"+(DateUtil.thisMonth()+1)+"-15")));
        if(!DateUtil.isIn(DateUtil.date(),DateUtil.beginOfDay(DateUtil.parseDate(DateUtil.thisYear()+"-"+(DateUtil.thisMonth()+1)+"-15")),DateUtil.endOfDay(DateUtil.parseDate(DateUtil.thisYear()+"-"+(DateUtil.thisMonth()+1)+"-25")))){
            throw new BusinessException(Type.FAIL.getCode(),  "提现周期为每月15-25日，目前暂无法发起提现申请，造成的不便敬请谅解");
        }
    }

    @Override
    public void add(CompanyWithdrawAddVO vo) {


        //校验提现参数
        if(vo.getWithdrawAmount().add(vo.getWithdrawCommission()).compareTo(vo.getWithdrawMoney())!=0){
            throw new BusinessException(Type.PARAM_VALIDATE_FAIL.getCode(),  "提现金额不匹配，目前暂无法发起提现申请，造成的不便敬请谅解");
        }
        //校验当前商户余额是否充足
        CompanyPO companyPO=companyDao.selectByPrimaryKey(StpUtil.getLoginId());
        if(companyPO.getIsWithdraw()==2){
            throw new BusinessException(Type.PARAM_VALIDATE_FAIL.getCode(),  "提现功能已被禁用，请联系管理员解除限制！");
        }
        Example example=new Example(CompanyWithdrawPO.class);
        example.and().andBetween("createDate",DateUtil.beginOfMonth(DateUtil.date()).toString(),DateUtil.endOfMonth(DateUtil.date()).toString());
        if(withdrawDao.selectCountByExample(example)>0){
            throw new BusinessException(Type.FAIL.getCode(),  "当月提现次数已用完，目前暂无法发起提现申请，造成的不便敬请谅解");
        }
//
//        if(!DateUtil.isIn(DateUtil.date(),DateUtil.beginOfDay(DateUtil.parseDate(DateUtil.thisYear()+"-"+(DateUtil.thisMonth()+1)+"-15")),DateUtil.endOfDay(DateUtil.parseDate(DateUtil.thisYear()+"-"+(DateUtil.thisMonth()+1)+"-25")))){
//            throw new BusinessException(Type.FAIL.getCode(),  "提现周期为每月15-25日，目前暂无法发起提现申请，造成的不便敬请谅解");
//        }


        if(vo.getWithdrawMoney().multiply(new BigDecimal("100")).compareTo(companyPO.getCompanyMoney())>=0){
            throw new BusinessException(Type.FAIL.getCode(),  "当前商户余额不足，目前暂无法发起提现申请，造成的不便敬请谅解");
        }
        CompanyWithdrawPO companyWithdrawPO=new CompanyWithdrawPO();
        BeanUtils.copyProperties(vo, companyWithdrawPO);
        companyWithdrawPO.preInsert();
        companyWithdrawPO.setId(DateUtil.format(new Date(),"yyyyMMddHHmmssSSS")+RandomUtil.randomInt(1000,9999));
        companyWithdrawPO.setWithdrawState(1);
        companyWithdrawPO.setCompanyId(companyPO.getId());
        //设置银行卡荣誉字段
        CompanyBankPO companyBankPO=companyBankDao.selectByPrimaryKey(vo.getBankId());
        companyWithdrawPO.setBankBranch(companyBankPO.getBankBranch());
        companyWithdrawPO.setBankName(companyBankPO.getBankName());
        companyWithdrawPO.setBankNo(companyBankPO.getBankNo());
        companyWithdrawPO.setBankContacts(companyBankPO.getBankContacts());
        //更新数据库
        int i = withdrawDao.insertSelective(companyWithdrawPO);
        if (i == Constants.SQL_RETURN_SUCCESS) {
            throw new BusinessException(Type.FAIL);
        }
        companyPO.setCompanyMoney(companyPO.getCompanyMoney().subtract(vo.getWithdrawMoney().multiply(new BigDecimal("100"))));
        companyPO.preUpdate();
        int j = companyDao.updateByPrimaryKeySelective(companyPO);
        if (j == Constants.SQL_RETURN_SUCCESS) {
            throw new BusinessException(Type.FAIL);
        }
        messageService.addSysMsg("提现申请通知",companyPO.getCompanyName()+"已发起金额为"+companyWithdrawPO.getWithdrawAmount()+"元的提现申请，请尽快前往【提现申请】中进行处理。","/sys/withdraw",3);
    }

    @Override
    public CompanyWithdrawCountVO get_count() {
        CompanyWithdrawCountVO companyWithdrawCountVO=new CompanyWithdrawCountVO();
        CompanyPO companyPO=companyDao.selectByPrimaryKey(StpUtil.getLoginId());
        companyWithdrawCountVO.setBalance(companyPO.getCompanyMoney());
        companyWithdrawCountVO.setMoney(companyPO.getCompanyMoney().divide(new BigDecimal("100")));
        companyWithdrawCountVO.setPaidMoney(withdrawDao.getCountMoney(companyPO.getId(),2));
        companyWithdrawCountVO.setUnPaidMoney(withdrawDao.getCountMoney(companyPO.getId(),1));
        return companyWithdrawCountVO;
    }
}